
package com.game.kugou.sdk;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import com.kugou.game.sdk.api.common.IEventCode;
import com.kugou.game.sdk.api.common.IEventDataField;
import com.kugou.game.sdk.api.common.OnPlatformEventListener;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.api.online.KGPlatform;

/**
 * 描述:SDK事件回调监听类
 *
 * @author kugou
 * @since 2015年7月8日 下午3:54:35
 */
public class SDKEventListener implements OnPlatformEventListener {

    private Context mContext;

    public SDKEventListener(Context context) {
        mContext = context;
    }

    @Override
    public void onEventOccur(int eventCode, Bundle data) {
        try {
            switch (eventCode) {
                case IEventCode.ENTER_GAME_SUCCESS:
                    // 获取登录成功后的用户信息
                    User user = (User) data.getSerializable(IEventDataField.EXTRA_USER);
                    if (user != null) {
                        Log.d("demo", "openId:" + user.getOpenId());// 用户唯一标识
                        Log.d("demo", "accountname:" + user.getAccountName());// 用户名
                        Log.d("demo", "nickname:" + user.getNickName());// 昵称
                        Log.d("demo", "token:" + user.getToken());// 登录token，可用于服务端登录验证
                    }

                    // 发送登录SDK成功广播通知界面
                    LocalBroadcastManager.getInstance(mContext).sendBroadcast(
                            new Intent(MainActivity.ACTION_LOGIN_SDK_SUCCESS));
                    break;
                case IEventCode.ENTER_GAME_FAILED:
                    String errorMsg = data.getString(IEventDataField.EXTRA_ERROR_MESSAGE);
                    Toast.makeText(mContext, "登录SDK失败", Toast.LENGTH_SHORT).show();
                    break;
                case IEventCode.ACCOUNT_CHANGED_SUCCESS:
                    Log.d("demo", "切换账号成功");
                    break;
                case IEventCode.REGISTER_SUCCESS:
                    Log.d("demo", "注册成功");
                    break;
                case IEventCode.RECHARGE_SUCCESS:
                    Log.d("demo", "充值成功");
                    break;
                case IEventCode.INTENT_TO_REBOOT_APP:
                    Log.d("demo", "即将重启游戏");
                    break;
                case IEventCode.GO_BACK_TO_GAME:
                    User info = KGPlatform.getCurrentUser();
                    if (info != null) {// 已登录SDK
                        Toast.makeText(mContext, "退出sdk回到游戏", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(mContext, "退出sdk回到游戏(还未登录)", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
